import pymysql


class mysql_connection:

    def __init__(self, db_connection):
        self.conn = pymysql.connect(host=db_connection["db_connection_host"],  # 数据库主机地址
                                    port=db_connection["db_connection_port"],  # 主机端口
                                    user=db_connection["db_connection_user"],  # 数据库用户名
                                    password=db_connection["db_connection_password"],  # 数据库密码
                                    db=db_connection["db_connection_db"],  # 数据库名
                                    charset="utf8mb4"  # 字符集
                                    )

    def select(self, sql):
        try:
            with self.conn.cursor() as cursor:
                # 执行 SQL 查询
                cursor.execute(sql)
                # 提交事务
                self.conn.commit()
                # 获取所有记录列表
                results = cursor.fetchall()
                return results
        finally:
            self.conn.close()  # 关闭连接
